--  护国远交
local huguoyuanjiao = fk.CreateSkill {
    name = "mcdh_huguoyuanjiao",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

huguoyuanjiao:addEffect('viewas',{
    pattern = "slash",
    card_filter = function(self, player,to_select, selected)
        return #selected == 0 and Fk:getCardById(to_select).type ~= Card.TypeBasic
    end,
    view_as = function(self, player,cards)
        if #cards ~= 1 then return nil end
        local card = Fk:cloneCard(self.pattern)
        card.skillName = self.name
        card:addSubcards(cards)
        return card
    end,
    enabled_at_play = function(self, player)
        return player.phase == Player.Play and player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,
    enabled_at_response = function(self, player, response)
        return false
    end,
})

huguoyuanjiao:addEffect(fk.TargetSpecified,{
    can_refresh = function(self, event, target, player, data)
        return data.card.skillName == "mcdh_huguoyuanjiao" and target == player
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local targets = data:getAllTargets()
        local flag = true
        for _, p in ipairs(targets) do
            if not player:inMyAttackRange(p) then
                flag = false
            end
        end
        if flag then
            if not data.use.extraUse then
                player:addCardUseHistory(data.card.trueName, -1)
                data.use.extraUse = true
            end
        end
    end
})

huguoyuanjiao:addEffect('targetmod',{
    bypass_distances = function(self, player, skill, card, target)
        return card and table.contains(card.skillNames, "mcdh_huguoyuanjiao")
    end,
    bypass_times = function (self, player, skill, scope, card, to)
        if card and table.contains(card.skillNames, "mcdh_huguoyuanjiao") then
            return player:inMyAttackRange(to)
        end
    end
})

Fk:loadTranslationTable {
    ["mcdh_huguoyuanjiao"] = "护国",
    [":mcdh_huguoyuanjiao"] = "『护国远交』<br>出牌阶段限一次，你可以将一张非基本牌当无距离限制的【杀】使用，你以此法对攻击范围内的角色使用【杀】无次数限制，此【杀】指定目标后，若目标角色在你的攻击范围内，此【杀】不计入次数。",
}

return huguoyuanjiao